package com.autologistics.action;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

import com.autologistics.model.Admin;
import com.autologistics.model.User;
import com.autologistics.service.AdminService;
import com.autologistics.service.UserService;
import com.autologistics.util.GobalValue;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private AdminService adminService;
	private UserService userService;
	private String account;
	private String pwd;
	private String juese;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getJuese() {
		return juese;
	}

	public void setJuese(String juese) {
		this.juese = juese;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public String adminLogin() {
		if (juese.equals("2")) {

			Admin a = adminService.getByAccoint(account);
			if (a == null) {
				message = "账号错误，无此用户";
				return "fail";
			}
			System.out.println(a.getPwd());
			Map<String, Object> attibutes = ActionContext.getContext()
					.getSession();
			String charSet = ActionContext.getContext().getSession()
					.get("md5RandomKey").toString();
			if (pwd.equals(getMD5Str(a.getPwd() + charSet, null))) {
				attibutes = ActionContext.getContext().getSession();
				attibutes.put("account", a.getAccount());
				attibutes.put("pwd", a.getPwd());
				attibutes.put("name", a.getName());
				attibutes.put("role", a.getRole());
				attibutes.put("id", a.getId());
				return SUCCESS;
			} else {
				message = "密码错误";

				return "fail";
			}
		} else {
			System.out.println("adminLogin");
			// 问题1：如果连这个账号都没有的话就是错的，
			User u = userService.getByAccount(account);
			if (u == null) {
				message = "账号错误，无此用户";
				return "fail";
			}
			System.out.println("from jsp:::account: " + account + "pwd   :"
					+ pwd);
			Map<String, Object> attibutes = ActionContext.getContext()
					.getSession();
			String charSet = ActionContext.getContext().getSession()
					.get("md5RandomKey").toString();
			System.out.println("charSet:" + charSet);
			String passwo = getMD5Str(u.getPwd() + charSet, null);
			System.out.println(passwo);
			if (pwd.equals(getMD5Str(u.getPwd() + charSet, null))) {
				attibutes = ActionContext.getContext().getSession();
				attibutes.put("account", u.getAccount());
				attibutes.put("pwd", u.getPwd());
				attibutes.put("name", u.getName());
				attibutes.put("id", u.getId());
				attibutes.put("userName", u.getUserName());
				return "user";
			} else {
				message = "密码错误";
				return "fail";
			}
		}
	}

	public String loginOut() {
		Map<String, Object> attibutes = ActionContext.getContext().getSession();
		ActionContext.getContext().getSession().clear();
		attibutes.clear();
		return SUCCESS;
	}

	/**
	 * md5����(ITS)
	 * 
	 * @param str
	 * @param charSet
	 * @return
	 */
	public synchronized static final String getMD5Str(String str, String charSet) { // md5����
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			if (charSet == null) {
				messageDigest.update(str.getBytes());
			} else {
				messageDigest.update(str.getBytes(charSet));
			}
		} catch (NoSuchAlgorithmException e) {
			LOG.error("md5 error:" + e.getMessage(), e);
		} catch (UnsupportedEncodingException e) {
			LOG.error("md5 error:" + e.getMessage(), e);
		}

		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
				md5StrBuff.append("0").append(
						Integer.toHexString(0xFF & byteArray[i]));
			else
				md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
		}
		return md5StrBuff.toString();
	}

	// 无session，跳转到login。jsp
	public String login() {
		return GobalValue.FAIL;
	}
}
